package com.csust.senior;

import com.csust.senior.dao.BaseDAO;
import com.csust.senior.dao.EmployeeDao;
import com.csust.senior.dao.bankDao;
import com.csust.senior.dao.impl.EmployeeDaoImpl;
import com.csust.senior.dao.impl.bankDAOImpl;
import com.csust.senior.pojo.Employee;
import com.csust.senior.utils.JDBCUtil;
import com.csust.senior.utils.JDBCUtilsV2;
import org.junit.Test;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class JDBCUtilsTest {
    @Test
    public void test() {
        Connection conn = JDBCUtil.getConnection();

        System.out.println(conn);

        JDBCUtil.release(conn);


    }

    @Test
    public void test1() {
//        Connection conn1 = JDBCUtil.getConnection();
//        Connection conn2 = JDBCUtil.getConnection();
//        Connection conn3 = JDBCUtil.getConnection();
//        System.out.println(conn1);
//        System.out.println(conn2);
//        System.out.println(conn3);


        Connection conn1 = JDBCUtilsV2.getConnection();
        Connection conn2 = JDBCUtilsV2.getConnection();
        Connection conn3 = JDBCUtilsV2.getConnection();


        System.out.println(conn1);
        System.out.println(conn2);
        System.out.println(conn3);









    }


    @Test
    public void test2() {
        EmployeeDao empdao = new EmployeeDaoImpl();

        List<Employee> list = empdao.SelectAll();
        for (Employee emp : list) {
            System.out.println(emp);
        }


    }

    @Test
    public void test3() {
        EmployeeDao empdao = new EmployeeDaoImpl();

        Employee employee = empdao.getEmployeeById(1);
        System.out.println(employee);

    }

    @Test
    public void test4() {
        EmployeeDao empdao = new EmployeeDaoImpl();

        Employee employee = new Employee(6,"Sail",25000.00,20);
        Employee employee1 = new Employee(0,"Kals",25000.00,20);

        int insert = empdao.insertEmployee(employee1);
        System.out.println("insert:"+insert);


    }
    @Test
    public void test5() {
        EmployeeDao empdao = new EmployeeDaoImpl();
        Employee employee1 = new Employee(6,"yuwen",29000.00,20);

        int update = empdao.updateEmployee(employee1);
        System.out.println("update:"+update);

    }
    @Test
    public void test6() {
        EmployeeDao empdao = new EmployeeDaoImpl();
//        Employee employee1 = new Employee(6,"yuwen",29000.00,20);
        int id = 6;
        int delete = empdao.deleteEmployee(id);
        System.out.println("delete:"+delete);

    }

    @Test
     public void testTransaction() {
        bankDao bd = new bankDAOImpl();
        Connection conn = null;
        try {
            conn = JDBCUtilsV2.getConnection();

            conn.setAutoCommit(false);

            bd.subBalance(1,100);

            int i = 10/0;
            bd.addBalance(2,100);


            conn.commit();

        } catch (Exception e) {
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }finally {
            JDBCUtilsV2.release();
        }



    }
}
